package com.dbh.alg.algorithm.recursion;

/**
 * 二分查找
 * @author 董帮辉
 * @date 2025/4/19
 **/
public class E03BinarySearch {

    public static int binarySearch(int[] arr, int target) {
        return f(arr, target, 0, arr.length - 1);
    }

    private static int f(int[] arr, int target, int i, int j) {
        if (i > j) {
            return -1;
        }
        int m = (i + j) >>> 1;
        if (target < arr[m]) {
            return f(arr, target, i, m - 1);
        } else if (target > arr[m]) {
            return f(arr, target, m + 1, j);
        } else {
            return m;
        }
    }


}
